<?php 
@header("Content-Type:text/html;charset=utf-8");
@include_once("conn.php");
@include_once("common.php");

if(!(isset($_GET["key"])&&isset($_GET["orderCol"])&&isset($_GET["orderType"])&&isset($_GET["pageIndex"])&&isset($_GET["showNum"]))){
    paramsErr();
}

$key=$_GET["key"];
$orderCol=$_GET["orderCol"];//排序列名
$orderType=$_GET["orderType"];
$pageIndex=$_GET["pageIndex"];
$showNum=$_GET["showNum"];


$sql="select count(*) as count from `goodslist` where goodsName like '%$key%'";
// $sql="select * from `goodslist` where goodsName like '%${key}%'";
$resultAll=mysqli_query($conn,$sql);
$obj=array();

    if($resultAll){ // 判断查询的结果
    
        $item = mysqli_fetch_assoc($resultAll);
        $count=$item["count"];

        $maxPage=ceil($count/$showNum);
        if($pageIndex>$maxPage){
            $pageIndex=$maxPage;
        }
        if($pageIndex<1){
            $pageIndex=1;
        }
        $skipNum=($pageIndex-1)*$showNum;
        $sql="select * from `goodslist` where goodsName like '%$key%' order by $orderCol $orderType limit $skipNum,$showNum";
        $result=mysqli_query($conn,$sql);
        if($result){
            $list=array();
            while($item=mysqli_fetch_assoc($result)){
                array_push($list,$item);
            }
            $obj["status"]=true;
            $obj["msg"]="success";
            $obj["count"]=$count*1;
            $obj["maxPage"]=$maxPage;
            $obj["currentIndex"]=$pageIndex;
            $obj["list"]=$list;

        }else{
            $obj["status"] = false;
            $obj["msg"] = "sql语句有误";
            $obj["sql"] = $sql;
        }
   
    }else{
        $obj["status"] = false;
        $obj["msg"] = "sql语句有误";
        $obj["sql"] = $sql;
    }
echo json_encode($obj);



// @header("Content-Type:text/html;charset=utf-8");
// @include_once("conn.php");
// @include_once("common.php");

// if(!(isset($_GET["key"]))){
//     paramsErr();
// }

// $key=$_GET["key"];
// $sql="select * from `goodslist` where goodsName like '%${key}%'";
// $result=mysqli_query($conn,$sql);
// $obj=array();

//     if($result){ // 判断查询的结果
    
//         $list = array();
//         while($item = mysqli_fetch_assoc($result)){  // 循环解析数据  有数据 => 继续解析,没有数据=> false => 跳出循环
//             // 数据的预处理
//            array_push($list,$item);
//         }

//         $obj["status"] = true;
//         $obj["msg"] = "success";
//         $obj["list"]=$list;
   
//     }else{
//         $obj["status"] = false;
//         $obj["msg"] = "sql语句有误";
//         $obj["sql"] = $sql;
//     }
// echo json_encode($obj);
?>